//
//  SLMessageCenterViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/11.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLMessageCenterViewController.h"
#import "SLMessageCenterViewModel.h"
#import "SLMsgTableViewCell.h"
#import <MJRefresh.h>
#import "SLProductDetailViewController.h"
#import "SLBoatDetailViewController.h"
#import "BKJH5ViewController.h"


@interface SLMessageCenterViewController ()<UITableViewDelegate, UITableViewDataSource,SLMsgTableViewCellDelegate>

@property (nonatomic, strong)SLMessageCenterViewModel *msgVM;

@property (nonatomic, strong)UITableView *msgTableView;

@end

@implementation SLMessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

  [self.view addSubview:self.msgTableView];
  [self.msgTableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
  }];
  
  [self addObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.title = @"最新消息";
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)addObservers{
  @weakify(self)
//  self.msgTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//    @strongify(self)
//    [self.msgVM getMsgList];
//  }];
  
  
  [[RACObserve(self.msgVM, msgList) filter:^BOOL(NSMutableArray  *value) {
    return value != nil;
  }] subscribeNext:^(NSMutableArray  *msgList) {
    @strongify(self)
    [self.msgTableView reloadData];
    [SVProgressHUD dismiss];
  }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - SLMsgTableViewCellDelegate

- (void)didtapedMsgCellDeletBtn:(SLMessageModel *)model{
  [self.msgVM deleteMsgWithMid:model.mid];
}


#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.msgVM.msgList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 120;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  SLMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  cell.delegate = self;
  return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SLMsgTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  [cell configMsgCellWithModel:self.msgVM.msgList[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLMessageModel *model = self.msgVM.msgList[indexPath.row];
  
  if ([model.type isEqualToString:@"1"]) {
    SLProductDetailViewController *goodsDetailVC = [SLProductDetailViewController new];
    [goodsDetailVC changeGid:model.gid];
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
  }else if ([model.type isEqualToString:@"2"]){
    SLBoatDetailViewController *boatDetailVC = [SLBoatDetailViewController new];
    [boatDetailVC changeBid:model.bid];
    [self.navigationController pushViewController:boatDetailVC animated:YES];
  }else if ([model.type isEqualToString:@"3"]){
    BKJH5ViewController *h5VC = [BKJH5ViewController new];
    h5VC.h5Url = model.url;
    [self.navigationController pushViewController:h5VC animated:YES];
  }
  
  

}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无消息" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

#pragma mark - getter

- (SLMessageCenterViewModel *)msgVM{
  if (!_msgVM) {
    _msgVM = [[SLMessageCenterViewModel alloc] init];
  }
  return _msgVM;
}

- (UITableView *)msgTableView{
  if (!_msgTableView) {
    _msgTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _msgTableView.backgroundColor = [UIColor defeutBackgroudColor];
    _msgTableView.tableFooterView = [UIView new];
    _msgTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _msgTableView.delegate = self;
    _msgTableView.dataSource = self;
    _msgTableView.emptyDataSetSource = self;
    [_msgTableView registerNib:[UINib nibWithNibName:@"SLMsgTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
  }
  return _msgTableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
